//
//  UIView+rotateLayerAnimation.m
//  stokeEndStudy
//
//  Created by 王若聪 on 2017/3/10.
//  Copyright © 2017年 PwC. All rights reserved.
//

#import "UIView+rotateLayerAnimation.h"

@implementation UIView (rotateLayerAnimation)
- (void)performLayerAnimationWithLayerWidth:(CGFloat)width inset:(CGFloat)inset color:(UIColor*)layerColor{
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    [self.layer addSublayer:layer];
    
    layer.strokeColor = [UIColor whiteColor].CGColor;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.lineWidth = width;
    
    layer.frame = CGRectInset(self.bounds, inset, inset);
    
    layer.lineCap = kCALineCapRound;
    
    CALayer *maskLayer = [CALayer layer];
    maskLayer.contents = (__bridge id)([UIImage imageNamed:@"angle-mask"].CGImage);
    maskLayer.frame = layer.bounds;
    layer.mask = maskLayer;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width * 0.5 - 2, self.bounds.size.width * 0.5 - 2) radius:self.bounds.size.width * 0.36 startAngle:0 endAngle:M_PI   clockwise:YES];
    layer.path = path.CGPath;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.repeatCount = ULONG_MAX;
    animation.duration = 2.0;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.fromValue = @(0);
    animation.toValue = @(2 * M_PI);
    [layer addAnimation:animation forKey:nil];
}

- (void)removeAnimationLayer{
    for (CALayer *layer in self.layer.sublayers) {
        if ([layer isKindOfClass:[CAShapeLayer class]]) {
            layer.hidden = YES;
            [layer removeFromSuperlayer];
        }
    }
};

- (void)stopAnimation{
    for (CALayer *layer in self.layer.sublayers) {
        if ([layer isKindOfClass:[CAShapeLayer class]]) {
            [layer removeAllAnimations];
        }
    }
}

@end
